package com.dayatang.hrm.compensation.infra.hibernate;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.hibernate.HibernateSessionManager;

import com.dayatang.commons.repository.EntityRepositoryHibernateTapestry;
import com.dayatang.hrm.compensation.domain.CalculateRule;
import com.dayatang.hrm.compensation.domain.CalculateRuleSetting;
import com.dayatang.hrm.compensation.domain.CalculateRuleSettingRepository;

@SuppressWarnings("unchecked")
public class CalculateRuleSettingRepositoryImpl extends
		EntityRepositoryHibernateTapestry<CalculateRuleSetting, Long> implements
		CalculateRuleSettingRepository {

	public CalculateRuleSettingRepositoryImpl(
			HibernateSessionManager sessionManager) {
		super(CalculateRuleSetting.class, sessionManager);
	}

	@Override
	public CalculateRuleSetting get(CalculateRule calculateRule, Date queryDate) {
		List<CalculateRuleSetting> list = getSessionManager().getSession()
				.getNamedQuery("CalculateRuleSetting.get").setEntity("pRule",
						calculateRule).setTimestamp("pQueryDate", queryDate)
				.list();

		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public CalculateRuleSetting getPure(
			CalculateRuleSetting calculateRuleSetting) {
		getSessionManager().getSession().evict(calculateRuleSetting);
		return get(calculateRuleSetting.getId());
	}

}
